package com.example.demo.controller;

import com.example.demo.bean.User;
import com.example.demo.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户列表", notes = "用户列表")
    @GetMapping("")
    public List<User> findAll(){
        return userService.findAll();
    }

    @ApiOperation(value = "创建用户", notes = "创建用户")
    @PostMapping("")
    public User postUser(@RequestBody User user){
        return userService.save(user);
    }

    @ApiOperation(value = "获取用户信息", notes = "根据URL的id获取用户详细信息")
    @GetMapping("/{id}")
    public User findUserById(@PathVariable Long id){
        return userService.findUserById(id);
    }

    @ApiOperation(value = "更新用户", notes = "根据URL的id指定更新用户信息")
    @PutMapping("/{id}")
    public User putUser(@PathVariable Long id,@RequestBody User user){
        user.setId(id);
        return userService.updateUser(user);
    }

    @ApiOperation(value = "删除用户", notes = "根据URL的id删除用户")
    @DeleteMapping("/{id}")
    public String  deleteUser(@PathVariable Long id){
        userService.deleteUser(id);
        return "success";
    }

    @ApiIgnore
    @GetMapping("/hi")
    public String hi(){
        return "hi you !";
    }

}
